/*
    This file is part of open-wars <http://code.google.com/p/open-wars/>.

    open-wars is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    open-wars is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with open-wars.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <opengl-ui.h>

static OpenGLUI *openGLUI;

static void OpengLUIOnMouseMove(void*)
{
    openGLUI->renderer->Repaint();
}

static void OpenGLUIOnIdle(boost::any lt)
{

    int lastTime = boost::any_cast<int>(lt);
    std::list<InputInterfaceMouse>::iterator mouse;
    // Get the current mouse texture
    for (mouse = openGLUI->interfaceMouse.begin(); mouse != openGLUI->interfaceMouse.end(); mouse++)
    {
        if (mouse->mouseType == openGLUI->currentMouseType)
            break;
    }

    if (mouse->init)
    {
        mouse->init = false;
        mouse->curFrame = mouse->textures.begin();
        mouse->reverse = false;
        mouse->tickCount = 0;
        return;
    }

    if ((lastTime - mouse->tickCount) < mouse->ticksPerFrame)
        return;

    mouse->tickCount = lastTime;
    switch (mouse->animationType)
    {
        case MouseAnimation::Static:
            {
                // Nothing to do here.. only expanded for readability
            } break;
        case MouseAnimation::Loop:
            {
                mouse->curFrame++;
                if (mouse->curFrame == mouse->textures.end())
                    mouse->curFrame = mouse->textures.begin();
            } break;
        case MouseAnimation::Bounce:
            {
                if (mouse->curFrame == mouse->textures.begin())
                {
                    mouse->curFrame++;
                    mouse->reverse = false;
                } else {
                    if (mouse->reverse) {
                        mouse->curFrame--;
                    } else {
                        mouse->curFrame++;
                    }

                    if (mouse->curFrame == mouse->textures.end())
                    {
                        mouse->reverse = true;
                        mouse->curFrame--;
                    }
                }
            } break;
    }
    openGLUI->renderer->Repaint();
}

OpenGLUI::OpenGLUI()
{
    openGLUI = this;
	renderer = NULL;
}

void OpenGLUI::SetRenderer(IRendererInterface *renderer)
{
	this->renderer = renderer;
    renderer->OnIdle.push_back(OpenGLUIOnIdle);
}

void OpenGLUI::SetInputInterface(IInputInterface *iface)        
{ 
    inputInterface = iface;
    iface->SetOnMouseMove(OpengLUIOnMouseMove);
};

void OpenGLUI::Render()
{
    std::list<InputInterfaceMouse>::iterator mouse;
    // Get the current mouse texture
    for (mouse = interfaceMouse.begin(); mouse != interfaceMouse.end(); mouse++)
    {
        if (mouse->mouseType == currentMouseType)
            break;
    }

    if (mouse->init)
    {
        mouse->init = false;
        mouse->curFrame = mouse->textures.begin();
        mouse->reverse = false;
        mouse->tickCount = 0;
    }

    MousePosResult mp = inputInterface->GetMousePos();
    
    renderer->DrawTexture(*mouse->curFrame, (float)mp.mouseX, (float)mp.mouseY, 32, 32);

}

void OpenGLUI::AddWidget(UIWidget *widget)
{

}

void OpenGLUI::RemoveWidget(UIWidget *widget)
{

}

void OpenGLUI::RemoveAllWidgets(void)
{

}
